package com.powernode.business.controller;

import com.powernode.business.domain.Car;
import com.powernode.business.dto.CarDto;
import com.powernode.business.service.CarService;
import com.powernode.common.core.controller.BaseController;
import com.powernode.common.core.domain.AjaxResult;
import com.powernode.common.core.domain.model.LoginUser;
import com.powernode.common.core.page.TableDataInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("business/car")
public class CarController extends BaseController {

    @Autowired
    private CarService carService;

    @GetMapping("list")
    @ApiOperation("分页查询车辆信息")
    public TableDataInfo list(Car car){
        startPage();
        LoginUser loginUser = getLoginUser();
        List<Car> carList = carService.selectCarList(car,loginUser);
        return getDataTable(carList);
    }

    /**
     * 新增车辆
     * @param carDto
     * @return
     */
    @PostMapping
    @ApiOperation("添加车辆")
    public AjaxResult addCar(@RequestBody CarDto carDto){
        LoginUser loginUser = getLoginUser();
        return toAjax(carService.addCar(carDto,loginUser));
    }

    @GetMapping("{id}")
    @ApiOperation("回显车辆")
    public AjaxResult carInfo(@PathVariable("id") Integer id){
        return AjaxResult.success(carService.getCarInfo(id));
    }

    @PutMapping
    @ApiOperation("回显车辆")
    public AjaxResult carInfo(@RequestBody CarDto carDto){
        //获取当前登录账号存入库中修改者的对应数据
        LoginUser loginUser = getLoginUser();
        return toAjax(carService.updateCar(carDto,loginUser));
    }


    @DeleteMapping("{ids}")
    @ApiOperation("删除车辆信息")
    public AjaxResult remove(@PathVariable("ids") List<Integer> ids){
        LoginUser loginUser = getLoginUser();
        return toAjax(carService.deleteCar(ids,loginUser));
    }
}
